package cn.edu.zafu.bigdata.hdb;
import java.sql.SQLException;
import java.util.Scanner;

public class StudentManagerSystemSQL {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("======= 学生信息管理系统 =======");
            System.out.println("1. 添加学生信息");
            System.out.println("2. 检索学生信息");
            System.out.println("3. 更新学生信息");
            System.out.println("4. 删除学生信息");
            System.out.println("5. 退出程序");
            System.out.println("6. 显示数据库中的表");
            System.out.println("7. 创建新表");
            System.out.print("请输入操作编号 (1-7): ");
            int choice = scanner.nextInt();
            scanner.nextLine(); // 消耗换行符

            switch (choice) {
                case 1:
                    addStudent(scanner);
                    break;
                case 2:
                    searchStudent(scanner);
                    break;
                case 3:
                    updateStudent(scanner);
                    break;
                case 4:
                    deleteStudent(scanner);
                    break;
                case 5:
                    System.out.println("退出程序。");
                    scanner.close();
                    return;
                case 6:
                    showtable();
                    break;
                case 7:
                    createtable(scanner);
                    break;
                default:
                    System.out.println("无效的选项，请重新输入。");
            }
        }
    }

    private static void addStudent(Scanner scanner) {
        System.out.print("请输入学号: ");
        String id = scanner.nextLine();
        try {
            if (DatabaseManager.getStudent(id) != null) {
                System.out.println("学号重复，请重新输入。");
                return;
            }
            System.out.print("请输入姓名: ");
            String name = scanner.nextLine();
            System.out.print("请输入电话: ");
            String phone = scanner.nextLine();
            Student student = new Student(id, name, phone);
            DatabaseManager.addStudent(student);
            System.out.println("学生信息添加成功。");
        } catch (SQLException e) {
            System.out.println("数据库操作失败: " + e.getMessage());
        }
    }

    private static void searchStudent(Scanner scanner) {
        while (true) {
            System.out.print("请输入学号进行检索（输入QUIT退出）: ");
            String id = scanner.nextLine();
            if ("QUIT".equalsIgnoreCase(id)) {
                break;
            }
            try {
                Student student = DatabaseManager.getStudent(id);
                if (student != null) {
                    System.out.println(student);
                } else {
                    System.out.println("未找到该学生。");
                }
            } catch (SQLException e) {
                System.out.println("数据库操作失败: " + e.getMessage());
            }
        }
    }

    private static void updateStudent(Scanner scanner) {
        while (true) {
            System.out.print("请输入学号进行更新（输入QUIT退出）: ");
            String id = scanner.nextLine();
            if ("QUIT".equalsIgnoreCase(id)) {
                break;
            }
            try {
                Student student = DatabaseManager.getStudent(id);
                if (student != null) {
                    System.out.print("请输入新的姓名: ");
                    String name = scanner.nextLine();
                    System.out.print("请输入新的电话: ");
                    String phone = scanner.nextLine();
                    student = new Student(id, name, phone);
                    DatabaseManager.updateStudent(student);
                    System.out.println("学生信息更新成功。");
                } else {
                    System.out.println("未找到该学生。");
                }
            } catch (SQLException e) {
                System.out.println("数据库操作失败: " + e.getMessage());
            }
        }
    }

    private static void deleteStudent(Scanner scanner) {
        while (true) {
            System.out.print("请输入学号进行删除（输入QUIT退出）: ");
            String id = scanner.nextLine();
            if ("QUIT".equalsIgnoreCase(id)) {
                break;
            }
            try {
                Student student = DatabaseManager.getStudent(id);
                if (student != null) {
                    DatabaseManager.deleteStudent(id);
                    System.out.println("学生信息删除成功。");
                } else {
                    System.out.println("未找到该学生。");
                }
            } catch (SQLException e) {
                System.out.println("数据库操作失败: " + e.getMessage());
            }
        }
    }

    private static void showtable() {
        try {
            DatabaseManager.showtables();
        } catch (SQLException e) {
            System.out.println("数据库操作失败: " + e.getMessage());
        }
    }

    private static void createtable(Scanner scanner) {
        System.out.print("请输入你想创建的表名: ");
        String tab = scanner.nextLine();
        DatabaseManager.createTable(tab);
    }
}
